package presentacion.personal;

import clases.personal.Sueldo;
import javax.swing.table.DefaultTableModel;

public class frmConsultaPersonal extends javax.swing.JInternalFrame {

    private static int pagina;
    private static String sql;
    public static Sueldo objEmpleado;

    /**
     * Creates new form frmConsultaConcepto
     */
    public frmConsultaPersonal() {
        initComponents();
        setResizable(false);
        objEmpleado = new Sueldo();

        pagina = 1;
        sql = objEmpleado.SqlGeneralPersonal();
        generartabla();
        inicializar();

    }

    public void llenarDialogo(int fila) {
        dlgPersonal empleado = new dlgPersonal(null, true);
        objEmpleado.llenardatosPersona(tblempleado.getValueAt(fila, 1).toString());
        objEmpleado.llenardatoSueldo();
        objEmpleado.llenardatosEmpelado();
        empleado.llenardatos();
        empleado.visiblidad(false);
        empleado.setVisible(true);

    }

    public void mostrarEmpleado() {
        dlgPersonal empleado = new dlgPersonal(null, true);
        empleado.setVisible(true);
        empleado.visiblidad(true);

    }

    public static void generartabla() {
        objEmpleado.ConsultaEmpleado(new DefaultTableModel(), tblempleado, sql,
                lbltotalpaginas, lblpagina, pagina);

    }

    private boolean CambiarBoolean(String dato) {
        if (dato.equals("ACTIVO")) {
            return true;
        } else {
            return false;
        }
    }

    private void inicializar() {
        txtsueldo.setVisible(false);
        txtsueldo.setText("");
        cbocargo.setSelectedIndex(0);
        cbocargo.setVisible(false);
        txtpersonal.setText("");
        txtpersonal.setVisible(false);
        dtfecha.setVisible(false);
        dtfechasalida.setVisible(false);
        dtfecha.setDate(global.Util.StringaDate(global.Util.getFechaActual()));
        dtfechasalida.setDate(global.Util.StringaDate(global.Util.getFechaActual()));
        cboestado.setVisible(false);
        cboestado.setSelectedIndex(0);
        objEmpleado.cargo.listarCargo(cbocargo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblempleado = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        btnprimero = new javax.swing.JButton();
        btnatras = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jLabel4 = new javax.swing.JLabel();
        lblpagina = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbltotalpaginas = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnsiguiente = new javax.swing.JButton();
        btnultimo = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cbocriterio = new javax.swing.JComboBox();
        txtsueldo = new javax.swing.JTextField();
        cbocargo = new javax.swing.JComboBox();
        txtpersonal = new javax.swing.JTextField();
        dtfecha = new com.toedter.calendar.JDateChooser();
        cboestado = new javax.swing.JComboBox();
        dtfechasalida = new com.toedter.calendar.JDateChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblempleado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblempleado.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblempleadoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblempleado);

        jToolBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        btnprimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/primer.png"))); // NOI18N
        btnprimero.setFocusable(false);
        btnprimero.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnprimero.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnprimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnprimeroActionPerformed(evt);
            }
        });
        jToolBar1.add(btnprimero);

        btnatras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/atras.png"))); // NOI18N
        btnatras.setToolTipText("");
        btnatras.setFocusable(false);
        btnatras.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnatras.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnatras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnatrasActionPerformed(evt);
            }
        });
        jToolBar1.add(btnatras);
        jToolBar1.add(jSeparator1);

        jLabel4.setText("Pagina ");
        jToolBar1.add(jLabel4);

        lblpagina.setText("jLabel2");
        jToolBar1.add(lblpagina);

        jLabel5.setText(" de ");
        jToolBar1.add(jLabel5);

        lbltotalpaginas.setText("jLabel1");
        jToolBar1.add(lbltotalpaginas);
        jToolBar1.add(jSeparator2);

        btnsiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/siguiente.png"))); // NOI18N
        btnsiguiente.setFocusable(false);
        btnsiguiente.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnsiguiente.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsiguienteActionPerformed(evt);
            }
        });
        jToolBar1.add(btnsiguiente);

        btnultimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ultimos.png"))); // NOI18N
        btnultimo.setFocusable(false);
        btnultimo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnultimo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnultimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnultimoActionPerformed(evt);
            }
        });
        jToolBar1.add(btnultimo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "BUSQUEDA POR:", 0, 0, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setText("CRITERIO");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 30, -1, -1));

        cbocriterio.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TODO", "PERSONAL", "CARGO", "SUELDO", "FECHA INGRESO", "FECHA SALIDA", "ESTADO" }));
        cbocriterio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbocriterioActionPerformed(evt);
            }
        });
        jPanel1.add(cbocriterio, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 50, 110, -1));

        txtsueldo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtsueldoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtsueldoKeyTyped(evt);
            }
        });
        jPanel1.add(txtsueldo, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 130, -1));

        cbocargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "Servicio", "Docente" }));
        cbocargo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbocargoActionPerformed(evt);
            }
        });
        jPanel1.add(cbocargo, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 150, -1));

        txtpersonal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtpersonalKeyReleased(evt);
            }
        });
        jPanel1.add(txtpersonal, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 260, -1));

        dtfecha.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtfechaPropertyChange(evt);
            }
        });
        jPanel1.add(dtfecha, new org.netbeans.lib.awtextra.AbsoluteConstraints(168, 50, 120, -1));

        cboestado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "ACTIVO", "INACTIVO" }));
        cboestado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboestadoActionPerformed(evt);
            }
        });
        jPanel1.add(cboestado, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 50, 120, -1));

        dtfechasalida.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtfechasalidaPropertyChange(evt);
            }
        });
        jPanel1.add(dtfechasalida, new org.netbeans.lib.awtextra.AbsoluteConstraints(168, 50, 120, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnsiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsiguienteActionPerformed
        if (objEmpleado.objPaginacion.getTotalpaginas() > pagina) {
            pagina++;
            generartabla();
        }
    }//GEN-LAST:event_btnsiguienteActionPerformed

    private void btnultimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnultimoActionPerformed
        pagina = objEmpleado.objPaginacion.getTotalpaginas();
        generartabla();
    }//GEN-LAST:event_btnultimoActionPerformed

    private void btnatrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnatrasActionPerformed
        if (pagina > 1) {
            pagina--;
            generartabla();
        }
    }//GEN-LAST:event_btnatrasActionPerformed

    private void btnprimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnprimeroActionPerformed
        pagina = 1;
        generartabla();
    }//GEN-LAST:event_btnprimeroActionPerformed

    private void cbocriterioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbocriterioActionPerformed
        inicializar();
        if (cbocriterio.getSelectedItem() == "TODO") {
            pagina = 1;
            sql = objEmpleado.SqlGeneralPersonal();
            generartabla();
        } else if (cbocriterio.getSelectedItem() == "SUELDO") {
            txtsueldo.setVisible(true);
            txtsueldo.requestFocus();
        } else if (cbocriterio.getSelectedItem() == "CARGO") {
            cbocargo.setVisible(true);
            cbocargo.requestFocus();
        } else if (cbocriterio.getSelectedItem() == "PERSONAL") {
            txtpersonal.setVisible(true);
            txtpersonal.requestFocus();
        } else if (cbocriterio.getSelectedItem() == "FECHA INGRESO") {
            dtfecha.setVisible(true);
            sql = objEmpleado.SqlFechaIngreso(global.Util.DateaString(dtfecha.getDate()));
            generartabla();
            dtfecha.requestFocus();
        } else if (cbocriterio.getSelectedItem() == "FECHA SALIDA") {
            dtfechasalida.setVisible(true);
            sql = objEmpleado.SqlFechaSalida(global.Util.DateaString(dtfechasalida.getDate()));
            generartabla();
            dtfechasalida.requestFocus();
        }else if (cbocriterio.getSelectedItem() == "ESTADO") {
            cboestado.setVisible(true);
            cboestado.requestFocus();
        }
    }//GEN-LAST:event_cbocriterioActionPerformed

    private void txtsueldoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtsueldoKeyReleased
        if ((global.Validaciones.comprobarNulo(txtsueldo))) {
            pagina = 1;
            sql = objEmpleado.SqlSueldo(Integer.parseInt(txtsueldo.getText()));
            generartabla();
        } else {
            sql = objEmpleado.SqlGeneralPersonal();
        }
        generartabla();
    }//GEN-LAST:event_txtsueldoKeyReleased

    private void txtsueldoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtsueldoKeyTyped
        global.Validaciones.soloNumeros(evt, txtsueldo, 15);
    }//GEN-LAST:event_txtsueldoKeyTyped

    private void cbocargoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbocargoActionPerformed
        if (cbocargo.getSelectedIndex() != 0 && cbocargo.getSelectedIndex() != -1) {
            sql = objEmpleado.SqlCargo(cbocargo.getSelectedItem().toString());
            generartabla();
        } else {
            sql = objEmpleado.SqlGeneralPersonal();
            generartabla();
        }
    }//GEN-LAST:event_cbocargoActionPerformed

    private void tblempleadoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblempleadoMouseClicked
        if (evt.getClickCount() == 2) {
            llenarDialogo(tblempleado.getSelectedRow());

        }
    }//GEN-LAST:event_tblempleadoMouseClicked

    private void txtpersonalKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtpersonalKeyReleased
        global.Validaciones.convertirMayuscula(txtpersonal);
        if ((global.Validaciones.comprobarNulo(txtpersonal))) {
            pagina = 1;
            sql = objEmpleado.SqLPersonal(txtpersonal.getText());
            generartabla();
        } else {
            sql = objEmpleado.SqlGeneralPersonal();
        }
        generartabla();
    }//GEN-LAST:event_txtpersonalKeyReleased

    private void dtfechaPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtfechaPropertyChange
        if (cbocriterio.getSelectedItem() == "FECHA INGRESO") {
            sql = objEmpleado.SqlFechaIngreso(global.Util.DateaString(dtfecha.getDate()));
            generartabla();
       }
    }//GEN-LAST:event_dtfechaPropertyChange

    private void cboestadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboestadoActionPerformed
        if (cboestado.getSelectedIndex() != 0) {
            sql = objEmpleado.SqlEstado(CambiarBoolean(cboestado.getSelectedItem().toString()));
            generartabla();
        } else {
            sql = objEmpleado.SqlGeneralPersonal();
            generartabla();
        }
    }//GEN-LAST:event_cboestadoActionPerformed

    private void dtfechasalidaPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtfechasalidaPropertyChange
        if (cbocriterio.getSelectedItem() == "FECHA SALIDA") {
            sql = objEmpleado.SqlFechaSalida(global.Util.DateaString(dtfechasalida.getDate()));
            generartabla();
       }
    }//GEN-LAST:event_dtfechasalidaPropertyChange

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmConsultaPersonal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmConsultaPersonal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmConsultaPersonal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmConsultaPersonal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frmConsultaPersonal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnatras;
    private javax.swing.JButton btnprimero;
    private javax.swing.JButton btnsiguiente;
    private javax.swing.JButton btnultimo;
    private javax.swing.JComboBox cbocargo;
    private javax.swing.JComboBox cbocriterio;
    private javax.swing.JComboBox cboestado;
    private com.toedter.calendar.JDateChooser dtfecha;
    private com.toedter.calendar.JDateChooser dtfechasalida;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private static javax.swing.JLabel lblpagina;
    private static javax.swing.JLabel lbltotalpaginas;
    public static javax.swing.JTable tblempleado;
    private javax.swing.JTextField txtpersonal;
    private javax.swing.JTextField txtsueldo;
    // End of variables declaration//GEN-END:variables
}
